package com.hardware.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hardware.common.R;
import com.hardware.dto.ShopDto;
import com.hardware.entity.Customer;
import com.hardware.entity.Shop;
import com.hardware.entity.User;
import com.hardware.service.CustomerService;
import com.hardware.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.hardware.utils.RedisConstants.CACHE_CUSTOMER_KEY;

/**
 * 人员管理：客户资料
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;

    /**
     * 分页查询客户列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = CACHE_CUSTOMER_KEY,key = "#page +'_'+ #pageSize")
    public R<Page> page(int page, int pageSize, String name){
        return customerService.page(page,pageSize,name);

    }
    /**
     * 根据id查询客户
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Cacheable(value = CACHE_CUSTOMER_KEY,key = "#id")
    public R<User> get(@PathVariable Long id){
        User user = userService.getById(id);
        return R.success(user);
    }

    /**
     * 新增客户
     * @param user
     * @return
     */
    @PostMapping
    @CacheEvict(value = CACHE_CUSTOMER_KEY, allEntries = true)
    public R<String> save(@RequestBody User user){
        userService.save(user);
        return R.success("新增客户成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = CACHE_CUSTOMER_KEY, allEntries = true)
    public R<String> updateByid(@PathVariable Integer status, @RequestParam List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.in(User::getId,ids);
        List<User> collect = ids.stream().map((item) -> {
            User user = new User();
            user.setStatus(status);
            user.setId(item);
            return user;
        }).collect(Collectors.toList());
        userService.updateBatchById(collect);
        return R.success("更新成功");
    }

    /**
     * 修改客户
     * @param user
     * @return
     */
    @PutMapping
    @CacheEvict(value = CACHE_CUSTOMER_KEY, allEntries = true)
    public R<String> update(@RequestBody User user){

        userService.updateById(user);

        return R.success("修改客户成功");
    }

    /**
     * 删除客户
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = CACHE_CUSTOMER_KEY, allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){

        userService.removeByIds(ids);

        return R.success("客户数据删除成功");
    }
}
